#!/usr/bin/env python
# coding: utf-8

# # 百度AI开放平台
# ## 通用物体和场景识别
# * 接口描述
# > 该请求用于通用物体及场景识别，即对于输入的一张图片（可正常解码，且长宽比适宜），输出图片中的多个物体及场景标签。
# 
# * 请求示例
# > HTTP 方法：POST
# 
# * 请求URL
# > https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general

# ### 鉴权认证机制
# * 获取Access Token

# In[3]:



# encoding:utf-8
import requests 

# client_id 为官网获取的AK， client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?'
# 准备url参数（酬载）
payload = {
    'grant_type':'client_credentials',
    'client_id':'VgHNKRwYOwbjDwYmY8I2jEam',
    'client_secret':'CG2W9YaWTSEH292jKfOAIsc64PcM3M7m'
    
    
}
response = requests.get(host, params=payload )
if response:
    print(response.json())
    access_token = response.json()['access_token']


# In[4]:


access_token


# * 请求参数

# In[5]:


import pandas as pd


# In[6]:


pd.read_html('https://cloud.baidu.com/doc/IMAGERECOGNITION/s/Xk3bcxe21')[2] #调用第三个表格


# In[8]:


# encoding:utf-8
import requests
import base64

'''
通用物体和场景识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"
# 二进制方式打开图片文件
f = open('xihu.jpg','rb')
img = base64.b64encode(f.read())

# params = {"image":img}
# access_token = '[调用鉴权接口获取的token]'
# request_url = request_url + "?access_token=" + access_token
# 写url的参数（酬载）
payload = {
    'access_token':access_token,
    'image':img,
    
}
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=payload, headers=headers)
if response:
    print (response.json())


# ## 菜品识别

# In[13]:


# encoding:utf-8
import requests
import base64

'''
菜品识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/dish"
# 二进制方式打开图片文件
f = open('xia.jpg','rb')
img = base64.b64encode(f.read())

# params = {"image":img}
# access_token = '[调用鉴权接口获取的token]'
# request_url = request_url + "?access_token=" + access_token
# 写url的参数（酬载）
payload = {
    'access_token':access_token,
    'image':img,
    'baike_num':5,
    'top_num':5
}
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=payload, headers=headers)
if response:
    print (response.json())


# In[7]:


import requests
import base64

'''
植物识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant"
# 二进制方式打开图片文件
f = open('hua.jpg', 'rb')
img = base64.b64encode(f.read())

payload={
    'access_token':access_token,
    'image':img,
    'baike_num':5,
    'top_num':5
}


headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=payload, headers=headers)
if response:
    print (response.json())


# In[ ]:




